package io.renren.modules.quickService.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.app.form.AttachmentForm;
import io.renren.modules.quickService.dao.AttachmentDao;
import io.renren.modules.quickService.entity.AttachmentEntity;
import io.renren.modules.quickService.service.AttachmentService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Service("attachmentService")
public class AttachmentServiceImpl extends ServiceImpl<AttachmentDao, AttachmentEntity> implements AttachmentService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<AttachmentEntity> page = this.selectPage(
                new Query<AttachmentEntity>(params).getPage(),
                new EntityWrapper<AttachmentEntity>()
                        .eq(params.get("bizId") != null, "biz_id", params.get("bizId"))
                        .eq(params.get("bizType") != null, "biz_type", params.get("bizType"))
        );

        return new PageUtils(page);
    }


    @Override
    public boolean saveAttachments(List<AttachmentForm> attachmentsForms, String bizId, String bizType) {
        try {
            if (attachmentsForms != null && attachmentsForms.size() > 0) {
                for (AttachmentForm form : attachmentsForms) {
                    AttachmentEntity attachmentEntity = new AttachmentEntity();
                    attachmentEntity.setBizId(bizId);
                    attachmentEntity.setAttachmentUrl(form.getAttachmentURL());
                    attachmentEntity.setBizType(bizType);
                    attachmentEntity.setAttachmentDesc(form.getAttachmentDesc());
                    attachmentEntity.setCreateTime(new Date());
                    baseMapper.insert(attachmentEntity);
                }
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public List<AttachmentEntity> attachmentList(String bizType, String bizId) {
        AttachmentEntity queryEntity = new AttachmentEntity();
        queryEntity.setBizType(bizType);
        queryEntity.setBizId(bizId);
        return baseMapper.attachmentList(queryEntity);
    }
}
